<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="配件编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="accessoryNum">
              <j-popup
                v-model="model.accessoryNum"
                field="accessoryNum"
                org-fields="code_num,name,style,cate_id,count_weight_pattern,price,weight,count_price_pattern,per,karat,attribute"
                dest-fields="accessoryNum,name,style,cateName,countWeightPattern,unitPrice,unitWeight,countPricePattern,per,karat,attribute"
                code="accessory_info_report"
                :multi="false"
                @input="popupCallback"
                />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="款号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="styleInfoId">
              <j-dict-select-tag type="list" v-model="model.styleInfoId" dictCode="style_info,code_num,id" placeholder="请输入款号" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="配件名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入配件名称" readOnly></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="style">
              <a-input v-model="model.style" placeholder="请输入规格" readOnly></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="类别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cateName">
              <j-dict-select-tag type="list" v-model="model.cateName" dictCode="accessory_category,cate_name,id" placeholder="请选择类别" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="count">
              <a-input-number v-model="model.count" placeholder="请输入数量"  :min="0" :precision="0" style="width:100%"></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="单价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unitPrice">
              <a-input-number v-model="model.unitPrice" placeholder="请输入单价" readOnly style="width:100%"></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="总工价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="labourTotalCost">
              <a-input-number v-model="model.labourTotalCost" placeholder="请输入总工价" style="width:100%"></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="单重" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unitWeight">
              <a-input-number v-model="model.unitWeight" placeholder="请输入单重" style="width:100%"></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="总重" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totalWeight">
              <a-input-number v-model="model.totalWeight" placeholder="请输入总重" style="width:100%"></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="长度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="len">
              <a-input-number v-model="model.len" placeholder="请输入长度" style="width:100%"></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="per">
              <a-input v-model="model.per" placeholder="请输入单位" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="计重方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="countWeightPattern">
              <j-dict-select-tag type="list" v-model="model.countWeightPattern" dictCode="count_weight_pattern" placeholder="请选择计重方式" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="计价方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="countPricePattern">
              <j-dict-select-tag type="list" v-model="model.countPricePattern" dictCode="count_price_pattern" placeholder="请选择计价方式" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="成色" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="karat">
              <j-dict-select-tag type="list" v-model="model.karat" dictCode="gold_karat,gold_karat_name,id" placeholder="请选择成色" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="属性" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attribute">
              <a-input v-model="model.attribute" placeholder="请输入属性" readOnly></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="计耗" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isAttrition">
              <j-dict-select-tag type="radio" v-model="model.isAttrition" dictCode="yn" placeholder="是否计耗" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" placeholder="请输入备注" ></a-textarea>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: "StyleAccessoryInfoModal",
    components: {
    },
    props:{
      mainId:{
        type:String,
        required:false,
        default:''
      }
    },
    data () {
      return {
        title:"操作",
        width:800,
        visible: false,
        model:{
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules: {
          count: [
            { required: true, message: '请输入数量!'}
          ]
        },
        url: {
          add: "/styleInfo/styleInfo/addStyleAccessoryInfo",
          edit: "/styleInfo/styleInfo/editStyleAccessoryInfo",
        }

      }
    },
    created () {
    //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    watch: {
      'model.unitWeight' : function() {
        this.setTotalWeight()
      },
      'model.count' : function() {
        this.setTotalWeight()
      }
    },
    methods: {
      setTotalWeight () {
        if(this.model.unitWeight != null && this.model.count != null && this.model.unitWeight !='' && this.model.count != '') {
          this.model.totalWeight = this.model.unitWeight * this.model.count
        } else {
          this.model.totalWeight = 0
        }
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.model['styleInfoId'] = this.mainId
        this.visible = true;
      },
      close () {
        this.$emit('close');
        this.visible = false;
        this.$refs.form.clearValidate();
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            this.model['styleInfoId'] = this.mainId
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }else{
             return false
          }
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(value,row){
       this.model = Object.assign(this.model, row);
      },


    }
  }
</script>
